<?php

namespace App\Http\Controllers\Admin;

use App\Models\Book;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BooksController extends Controller
{
    public function search(Request $request)
    {
        $q = $request->input('q');
        $book_id = $request->input('book_id');

        $builder = Book::query();
        if(!empty($book_id))
        {
            $builder->where('id',$book_id);
        }
        if(!empty($q))
        {
            $builder->where('title','like',"%$q%");
        }

        return $builder->paginate(null, ['id', 'title as text']);
    }

    public function searchBook($book_id, Request $request)
    {
        $q = $request->input('q');

        $builder = Book::query();
        if(!empty($book_id))
        {
            $builder->where('id',$book_id);
        }
        if(!empty($q))
        {
            $builder->where('title','like',"%$q%");
        }

        $builder->where('deleted',0);

        $items = [];
        $list = $builder->get();
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                $items[] = [
                    'id' => $v['id'],
                    'text' => $v['title']
                ];
            }
        }

        return $items;
    }
}
